package com.atguigu.tingshu.order.config;

import com.atguigu.tingshu.common.rabbit.constant.MqConst;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;


@Configuration
public class RabbitAutoConfiguration {
    /**
     * 定义延迟交换机
     *  String name :交换机的名字
     *  String type ：减缓基类型
     *  boolean durable：交换机是否持久化(rabbitmq服务重启之后交换机的名字 类型 等元数据是否还存在)
     *  boolean autoDelete：交换机是否自动删除，(如果交换机没有队列与其绑定，这个交换机就没有)
     *  Map<String,Object> arguments:交换机的参数
     *
     *
     */
    @Bean
    public CustomExchange customExchange(){
        Map<String,Object> args = new HashMap<>();
        args.put("x-delayed-type","direct"); //x-delayed-type
        return new CustomExchange(MqConst.EXCHANGE_CANCEL_ORDER,"x-delayed-message",true,false,args);
    }

    /**
     * 定义正常的队列
     */
    @Bean
    public Queue delayQueue(){
        return new Queue(MqConst.QUEUE_CANCEL_ORDER,true,false,false);
    }

    /**
     * 定义延时交换机和正常队列的绑定
     */

    @Bean
    public Binding delayBinding(CustomExchange customExchange,Queue delayQueue){
        return BindingBuilder.bind(delayQueue).to(customExchange).with(MqConst.ROUTING_CANCEL_ORDER).noargs();
    }

}
